package com.fare.taluo.core.domain.crawl.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.dayatang.hibernate.EntityRepositoryHibernate;
import com.fare.taluo.core.domain.crawl.TaskAdapterEntity;
import com.fare.taluo.core.domain.crawl.TaskAdapterEntityRepository;
import com.fare.taluo.util.StringUtils;

@Repository
public class TaskAdapterEntityRepositoryImpl 
		extends EntityRepositoryHibernate 
		implements  TaskAdapterEntityRepository {

	@Override
	public void deleteTaskAdapterEntity(TaskAdapterEntity tae) throws Exception {
		super.remove(tae);
	}

	@Override
	public void saveOrUpdateTaskAdapterEntity(TaskAdapterEntity tae)
			throws Exception {
		super.save(tae);
	}
	@Override
	public void saveMorU(TaskAdapterEntity tae)
			throws Exception {
		super.save(tae);
	}
	@Override
	public TaskAdapterEntity findTaskAdapterEntity(long id) throws Exception {
		return super.get(TaskAdapterEntity.class, id);
	}

	
	@Override
	public List<TaskAdapterEntity> findTaskAdapterByConsdions(
			String sourceId,String isEnable, String adapterName,String adapterClass){
		
		StringBuffer sbf=new StringBuffer("from TaskAdapterEntity tae where 1=1 ");
		List<Object> list = new ArrayList<Object>();
		//判断 渠道id
		if(StringUtils.isNotEmpty(sourceId)) {
			sbf.append("and tae.sourceId.id=? ");
			Long sourceID=Long.parseLong(sourceId);
			list.add(sourceID);
		}
		//判断 适配器状态
		if(StringUtils.isNotEmpty(isEnable)){
			sbf.append("and tae.isenable= ? ");
			list.add(isEnable);
		}
		//判断 适配器名称
		if(StringUtils.isNotEmpty(adapterName)){
			sbf.append("and tae.adapterName=? ");
			list.add(adapterName);
		}
		
		//判断  适配器路径和类名
		if(StringUtils.isNotEmpty(adapterClass)){
			sbf.append("and tae.adapterClass=? ");
			list.add(adapterClass);
		}
		//按更新的日期排序
		sbf.append("order by tae.updatetime desc");
		List<TaskAdapterEntity> lists=super.find(sbf.toString(),list.toArray(),TaskAdapterEntity.class);
		return lists;
	}

	@Override
	public List<TaskAdapterEntity> findTaskAdapterByConditions(Long sourceId,
			Integer isEnable, String adapterClassName) throws Exception {
		StringBuffer sb=new StringBuffer("from TaskAdapterEntity tae where 1=1 ");
		List<Object> params = new ArrayList<Object>();
		if(sourceId!=null){
			sb.append("and tae.sourceId.id=? ");
			params.add(sourceId);
		}
		if(isEnable!=null){
			sb.append("and tae.isenable= ? ");
			params.add(isEnable.toString());
		}
		if(adapterClassName!=null){
			sb.append("and tae.adapterClass=? ");
			params.add(adapterClassName);
		}
		sb.append("order by tae.sourceId.id");
		return super.find(sb.toString(), params.toArray(), TaskAdapterEntity.class);
		
	}
	
}
